package work.io.bio;


import sun.font.TextRecord;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class Server {


    public static void main(String[] args) {

        startServer();

        startClient();

    }

    private static void startServer() {

        new Thread(() -> {
            try {
                ServerSocket socket = new ServerSocket(8800);
                Socket accept = socket.accept();

                //创建两个对象流
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(accept.getOutputStream());
                ObjectInputStream objectInputStream = new ObjectInputStream(accept.getInputStream());


                //向目标主机发起连接
                Socket s = new Socket("localhost", 8213);

                OutputStream outputStream = s.getOutputStream();

                InputStream inputStream = s.getInputStream();


                new Thread(() -> {

                    while (true) {
                        try {
                            Object o = objectInputStream.readObject();
                            if (o instanceof ByteArrayMessage) {
                                outputStream.write(((ByteArrayMessage) o).getBody());
                            }
                        } catch (IOException | ClassNotFoundException e) {
                            e.printStackTrace();

                            break;
                        }
                    }

                }).start();


                new Thread(() -> {

                    byte[] bytes = new byte[1024];
                    int len;
                    ByteArrayMessage message = new ByteArrayMessage();
                    while (true) {

                        try {
                            len = inputStream.read(bytes);
                            if (len == 1024) {
                                message.setBody(bytes);
                                objectOutputStream.writeObject(message);
                            } else {
                                if (len == -1) {
                                    break;
                                } else {
                                    byte[] bs = new byte[len];
                                    System.arraycopy(bytes, 0, bs, 0, len);
                                    message.setBody(bs);
                                    objectOutputStream.writeObject(message);
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();

                            break;
                        }

                    }

                    System.out.println("线程终止");

                }).start();


            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();

    }


    /*

     */
    private static void startClient() {
        new Thread(() -> {
            try {
                ServerSocket socket = new ServerSocket(8080);
                Socket accept = socket.accept();

                OutputStream outputStream = accept.getOutputStream();
                InputStream inputStream = accept.getInputStream();

                //向服务端服务端发起请求
                Socket s = new Socket("localhost", 8800);
                ObjectInputStream objectInputStream = new ObjectInputStream(s.getInputStream());

                ObjectOutputStream objectOutputStream = new ObjectOutputStream(s.getOutputStream());

                new Thread(() -> {

                    while (true) {
                        try {
                            Object o = objectInputStream.readObject();
                            if (o instanceof ByteArrayMessage) {
                                outputStream.write(((ByteArrayMessage) o).getBody());
                            }
                        } catch (IOException | ClassNotFoundException e) {
                            e.printStackTrace();
                            break;
                        }
                    }

                }).start();


                new Thread(() -> {

                    byte[] bytes = new byte[1024];
                    int len;
                    ByteArrayMessage message = new ByteArrayMessage();
                    while (true) {

                        try {
                            len = inputStream.read(bytes);
                            if (len == 1024) {
                                message.setBody(bytes);
                                objectOutputStream.writeObject(message);
                            } else {
                                if (len == -1) {
                                    break;
                                } else {
                                    byte[] bs = new byte[len];
                                    System.arraycopy(bytes, 0, bs, 0, len);
                                    message.setBody(bs);
                                    objectOutputStream.writeObject(message);
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();

                            break;
                        }

                    }

                    System.out.println("线程终止");

                }).start();


            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();


    }


}
